#include "WaveManager.h"
#include "GameUtil.h"
#include "WaveEndedLayer.h"
#include "MainGameScene.h"
#include "Wave.h"

WaveManager::WaveManager(CCLayer* layer)
{
	_layer = layer;
	_winSize = CCDirector::sharedDirector()->getWinSize();  
	isSpawnEnd = true;

	// should get from preference
	data.currentWave = 1;
	waveData = NULL;
}

WaveManager::~WaveManager(void)
{
}

void WaveManager::spawnEnemys()
{
	GameUtil::instance()->getWaveManager()->spawnGroup();
}

void WaveManager::spawnBoss(float dt)
{
	GameUtil::instance()->getEnemyManager()->spawn(BOSS1, ccp(3, 3));
	isSpawnEnd = true;
}

void WaveManager::spawnGroup()
{

	// check max enemy
	int currentEnemyCount = GameUtil::instance()->getEnemyManager()->getEnemys()->count();
	if(currentEnemyCount + currentGData->d_enemyData->count() > MAX_ENEMY)
	{
		// wait 1s
		reSchedule(1.0f);
		return;
	}

	// spawn
	GameUtil::instance()->getGameStateLayer()->updateProgress(waveData->count(), currentGroup);
	CCObject *enemy = NULL;  
	CCARRAY_FOREACH(currentGData->d_enemyData, enemy)
	{
		OneEnemy* enemyData = (OneEnemy*)enemy;
		GameUtil::instance()->getEnemyManager()->spawn(enemyData->d_type, enemyData->d_pos);
	}
	
	// next group
	currentGroup++;
	if(currentGroup >= waveData->count()) {
		waveEnded();
		// spawn boss
		if (true){//data.currentWave%5 == 0) {
			isSpawnEnd = false;

			showBoss(0.0f);
			spawnBoss(0.0f);
			//this->schedule(schedule_selector(WaveManager::showBoss));
			//this->schedule(schedule_selector(WaveManager::spawnBoss));
		}
		return;
	}
	currentGData = (GroupData*)(waveData->objectAtIndex(currentGroup));
	reSchedule(currentGData->d_delay);
}

void WaveManager::startWave(void)
{
	// here reset player and teeth's power
	GameUtil::instance()->getToothManager()->updateTeeth();
	GameUtil::instance()->getPlayer()->onUpdrade();

	Setting* setting = GameUtil::instance()->getSetting();
	data.currentWave = setting->getWave();
	waveData = Wave::getWave(data.currentWave);

	data.getMoney = 0;
	data.kill = 0;
	data.damage = 0;
	data.hit = 0;
	data.getDamage = 0;
	data.startTime = time(NULL);

	showWave();
	//GameUtil::instance()->getGameStateLayer()->updateWaveText();
	
	isSpawnEnd = false;
	currentGroup = 0;
	currentGData = (GroupData*)(waveData->objectAtIndex(currentGroup));
	reSchedule(1.0f);

	// play background music
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/main.mp3", true);
}

void WaveManager::reSchedule(float delay)
{
	// todo schedule can't call itself
/*	_layer->unschedule(schedule_selector(WaveManager::spawnEnemys));
	_layer->scheduleOnce(schedule_selector(WaveManager::spawnEnemys), delay);  */ 


	CCDelayTime * delayAction = CCDelayTime::create(delay);
    CCCallFunc * callFunc = CCCallFunc::create(_layer, callfunc_selector(WaveManager::spawnEnemys));
    _layer->runAction( CCSequence::createWithTwoActions(delayAction, callFunc));

}

void WaveManager::waveEnded(void)
{
	Setting* setting = GameUtil::instance()->getSetting();
	setting->setWave(data.currentWave+1);
	isSpawnEnd = true;
	Wave::releaseWave(waveData);

	// show 
	//GameUtil::instance()->waveEnded();
}

void WaveManager::showWave(void)
{
	// show wave msg
	char msg[10];
	sprintf(msg, "WAVE %d", data.currentWave);
	_layer->removeChild(waveLabel);
	waveLabel = CCLabelTTF::create(msg, "Artial", 120);

	//waveLabel->setOpacity(255); 
	waveLabel->setColor(ccc3(51,181,229));         
	waveLabel->setPosition(ccp(_winSize.width + waveLabel->getContentSize().width/2, _winSize.height/2)); 
	_layer->addChild(waveLabel);

	waveLabel->runAction(CCSequence::create(
		CCEaseIn::create(CCMoveTo::create(1.0f, ccp(_winSize.width/2, _winSize.height/2)), 2.0f),
		CCDelayTime::create(2.0f),
		CCFadeOut::create(1.0f),
		CCCallFuncN::create(waveLabel, callfuncN_selector(WaveManager::waveMsgEnd)), //TODO no this
		NULL));

}

void WaveManager::showBoss(float dt)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/boss.mp3", true);
	// show wave msg
	char msg[10];
	sprintf(msg, "BOSS");
	_layer->removeChild(waveLabel);
	waveLabel = CCLabelTTF::create(msg, "Artial", 120);

	//waveLabel->setOpacity(255); 
	waveLabel->setColor(ccc3(0x99,0x0,0x56));         
	waveLabel->setPosition(ccp(_winSize.width + waveLabel->getContentSize().width/2, _winSize.height/2)); 
	_layer->addChild(waveLabel);

	waveLabel->runAction(CCSequence::create(
		CCEaseIn::create(CCMoveTo::create(1.0f, ccp(_winSize.width/2, _winSize.height/2)), 2.0f),
		CCDelayTime::create(2.0f),
		CCFadeOut::create(1.0f),
		CCCallFuncN::create(waveLabel, callfuncN_selector(WaveManager::waveMsgEnd)), //TODO no this
		NULL));

}

void WaveManager::waveMsgEnd(CCNode* sender)
{
}

void WaveManager::killed(int money, int hp)
{
	data.kill += 1;
	data.getMoney += money;
	data.damage += hp;
}

void WaveManager::hitted(int damage)
{
	data.hit += 1;
	data.getDamage += damage;
}

const WaveData & WaveManager::getWaveData()
{
	return data;
}
